//
//  ViewController.m
//  03-nsoperationqueue基本使用
//
//  Created by J on 16/2/24.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import "CustomOperation.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSOperationQueue* queue =[[NSOperationQueue alloc]init];
//    /*
//     主队列: [NSOperationQueue mainQueue]
//     其他队列: [[NSOperationQueue alloc]init](并发\串行)
//     */
//    
//    //并发队列(自己创建的\全局的)
//    //串行队列(主队列\自己创建的)
//    
//    
    NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoad) object:nil];

    [queue addOperation:op];//会自动start 并且开启一个子线程

    
    
    
    NSInvocationOperation* op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoad2) object:nil];
    //默认是并发队列
    
    [queue addOperation:op2];//会自动start 并且开启一个子线程
    
    NSBlockOperation* blockOp = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"NSBlockOperation1:%@",[NSThread currentThread]);
    }];
    [blockOp addExecutionBlock:^{
        NSLog(@"addExecutionBlock2:%@",[NSThread currentThread]);

        
    }];
    [blockOp addExecutionBlock:^{
        NSLog(@"addExecutionBlock3:%@",[NSThread currentThread]);
        
        
    }];
    [queue addOperation:blockOp];
//  [queue addOperation:blockOp]operation is already enqueued on a queue' 重复添加一个operation会崩溃

    
    
    
    NSBlockOperation* blockOp2 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"blockOp2:%@",[NSThread currentThread]);
    }];
    [queue addOperation:blockOp2];

    //创建自己的operation  当异步操作特别多 代码特别多 很多地方都要用 的时候就自定义
    
    CustomOperation* customBlockOp2 = [[CustomOperation alloc]init];
    [queue addOperation:customBlockOp2];


    

    
}

- (void)downLoad{
    NSLog(@"%@",[NSThread currentThread]);
}

- (void)downLoad2{
    NSLog(@"%@",[NSThread currentThread]);
}

@end
